﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FieldDevice.UI
{
    public partial class CurrentColumn : UserControl, IDisposable
    {
        public CurrentColumn()
        {
            InitializeComponent();
        }
        public Device Device1P1;
        public Device Device2P1;
        public Device Device1P11;
        public void UpdateReads()
        {
            this.Value_1P1[0] = this.Device1P1.CurrentReads["IA"];
            this.Value_1P1[1] = this.Device1P1.CurrentReads["IB"];
            this.Value_1P1[2] = this.Device1P1.CurrentReads["IC"];
            this.Value_1P11[0] = this.Device1P11.CurrentReads["IA"];
            this.Value_1P11[1] = this.Device1P11.CurrentReads["IB"];
            this.Value_1P11[2] = this.Device1P11.CurrentReads["IC"];
            this.Value_2P1[0] = this.Device2P1.CurrentReads["IA"];
            this.Value_2P1[1] = this.Device2P1.CurrentReads["IB"];
            this.Value_2P1[2] = this.Device2P1.CurrentReads["IC"];
            this.Refresh();
        }
        Font font = new Font(FontFamily.GenericSerif, 10);
        Brush textBrush = new SolidBrush(Color.Red);
        private Color gridColor = Color.Black;
        private Color curA_Color = Color.Yellow;
        private Color curB_Color = Color.Green;
        private Color curC_Color = Color.Red;

        private float[] value_1P1 = new float[3] { 0.00f, 0.00f, 0.00f };
        private float[] value_1P11 = new float[3] { 0.00f, 0.00f, 0.00f };
        private float[] value_2P1 = new float[3] { 0.00f, 0.00f, 0.00f };

        #region ///属性
        [Category("CurrentBar"), Description("柱状图的标题")]
        public string Title { get; set; }
        [Category("CurrentBar"), Description("框图区域的背景颜色")]
        public Color DiagramBackgroundColor
        {
            get { return this.BackColor; }
            set
            {
                this.BackColor = value;
            }
        }

        [Category("CurrentBar"), Description("框图区域的网格颜色")]
        public Color GridColor { get { return gridColor; } set { gridColor = value; } }
        [Category("CurrentBar"), Description("框图宽度")]
        public float DiagramWidth
        {
            get { return this.Width * 0.9f; }
        }
        [Category("CurrentBar"), Description("框图高度")]
        public float DiagramHeight
        {
            get { return this.Height * 0.6f; }
        }

        [Category("CurrentBar"), Description("A相电流颜色")]
        public Color A_Color
        {
            get { return curA_Color; }
            set { this.curA_Color = value; }
        }

        [Category("CurrentBar"), Description("B相电流颜色")]
        public Color B_Color
        {
            get { return curB_Color; }
            set { this.curB_Color = value; }
        }

        [Category("CurrentBar"), Description("C相电流颜色")]
        public Color C_Color
        {
            get { return curC_Color; }
            set { this.curC_Color = value; }
        }
        [Category("CurrentBar"), Description("横坐标上的零点位置")]
        public float X_axis_Zero
        {
            get { return 0.1f * this.Width; }
        }
        [Category("CurrentBar"), Description("纵坐标上的零点位置")]
        public float Y_axis_Zero
        {
            get { return 0.1f * this.Height; }
        }

        [Category("CurrentBar"),
         Description("1P1的三相电流,Value_1P1[0]为A相，Value_1P1[1]为B相,Value_1P1[2]为C相")]
        public float[] Value_1P1
        {
            get { return this.value_1P1; }
            set
            {
                this.value_1P1 = value;
                this.Paint += new System.Windows.Forms.PaintEventHandler(this.Drawing1P1Value);
                this.Refresh();
            }
        }
        [Category("CurrentBar"),
        Description("1P11的三相电流,Value_1P11[0]为A相，Value_1P11[1]为B相,Value_1P11[2]为C相")]
        public float[] Value_1P11
        {
            get { return this.value_1P11; }
            set
            {
                this.value_1P11 = value;
                this.Paint += new System.Windows.Forms.PaintEventHandler(this.Drawing1P11Value);
                this.Refresh();
            }
        }
        [Category("CurrentBar"),
         Description("2P1的三相电流,Value_2P1[0]为A相，Value_2P1[1]为B相,Value_2P1[2]为C相")]
        public float[] Value_2P1
        {
            get { return this.value_2P1; }
            set
            {
                this.value_2P1 = value;
                this.Paint += new System.Windows.Forms.PaintEventHandler(this.Drawing2P1Value);
                this.Refresh();
            }
        }

        #endregion
        private void CurrentColumn_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphic = e.Graphics;
            Font textFont = new Font(FontFamily.GenericSerif, 10);
            ///
            /// 画柱状图的外框
            ///
            graphic.DrawRectangle(new Pen(GridColor, 3), X_axis_Zero, 0.1f * this.Height, DiagramWidth, DiagramHeight);
            ///
            ///画网格线
            ///
            for (int i = 1; i <= 10; i++)
            {
                graphic.DrawLine(new Pen(GridColor), X_axis_Zero, 0.1f * this.Height + (i * DiagramHeight) / 9.0f
                            , this.Width, 0.1f * this.Height + (i * DiagramHeight) / 9.0f);
            }
            ///
            ///Y轴上的坐标值
            ///
            for (int i = 0; i <= 5; i++)
            {
                graphic.DrawString((i * 200).ToString("F2"), textFont, textBrush,
                                 new PointF(0.1f * Width - textFont.Size * 5,
                                    0.7f * this.Height - (i * DiagramHeight) / 5.0f - this.Font.Size));
            }
            graphic.DrawString("进线电流柱状图", new Font(FontFamily.GenericSerif, 14), textBrush,
                                new PointF(0.5f * this.Width - 8 * this.Font.Size, 0.05f * this.Height - this.Font.Size));
            graphic.FillRectangle(new SolidBrush(curA_Color), 0.7f * this.Width, 0.03f * this.Height, 0.03f * this.Width, 0.03f * this.Height);
            graphic.DrawString("A相电流", new Font(FontFamily.GenericSerif, 8), textBrush, 0.73f * this.Width, 0.03f * this.Height);
            graphic.FillRectangle(new SolidBrush(curB_Color), 0.8f * this.Width, 0.03f * this.Height, 0.03f * this.Width, 0.03f * this.Height);
            graphic.DrawString("B相电流", new Font(FontFamily.GenericSerif, 8), textBrush, 0.83f * this.Width, 0.03f * this.Height);
            graphic.FillRectangle(new SolidBrush(curC_Color), 0.9f * this.Width, 0.03f * this.Height, 0.03f * this.Width, 0.03f * this.Height);
            graphic.DrawString("C相电流", new Font(FontFamily.GenericSerif, 8), textBrush, 0.93f * this.Width, 0.03f * this.Height);
            DrawingListVal(graphic);
        }


        public void DrawingListVal(Graphics graph)
        {

            graph.DrawRectangle(new Pen(gridColor), 0.0f, 0.71f * this.Height, this.Width, 0.28f * this.Height);
            for (int j = 1; j <= 4; j++)
            {
                graph.DrawLine(new Pen(gridColor), 0.0f, (0.7f + j * 0.06f) * this.Height, this.Width, (0.7f + j * 0.06f) * this.Height);
            }
            for (int i = 1; i <= 3; i++)
            {
                graph.DrawLine(new Pen(gridColor), i * 0.25f * this.Width, 0.71f * this.Height, i * 0.25f * this.Width, 0.99f * this.Height);
            }
            graph.DrawString("线路编号", font, textBrush, 0.1f * this.Width, 0.73f * this.Height);
            graph.DrawString("A相电流", font, textBrush, 0.1f * this.Width, 0.79f * this.Height);
            graph.DrawString("B相电流", font, textBrush, 0.1f * this.Width, 0.85f * this.Height);
            graph.DrawString("C相电流", font, textBrush, 0.1f * this.Width, 0.91f * this.Height);
            graph.DrawString("额定电流", font, textBrush, 0.1f * this.Width, 0.96f * this.Height);
            graph.DrawString("1P1进线柜", font, textBrush, 0.35f * this.Width, 0.73f * this.Height);
            graph.DrawString("1P11进线柜", font, textBrush, 0.6f * this.Width, 0.73f * this.Height);
            graph.DrawString("2P1进线柜", font, textBrush, 0.85f * this.Width, 0.73f * this.Height);

            for (int count = 0; count < 3; count++)
            {
                graph.DrawString("3200.00", font, textBrush,
                    0.1f * this.Width + 0.25f * (count + 1) * this.Width, 0.96f * this.Height);
            }
        }

        public void Drawing1P1Value(object sender, PaintEventArgs e)
        {
            Graphics graph = e.Graphics;
            ///
            /// 1P1的柱状图
            ///
            graph.FillRectangle(new SolidBrush(curA_Color), 0.25f * this.Width, (0.6f * (1000 - value_1P1[0]) * this.Height) / 1000.0f + Y_axis_Zero,
                        0.08f * this.Width, 0.6f * value_1P1[0] * this.Height / 1000.0f);
            graph.FillRectangle(new SolidBrush(curB_Color), 0.33f * this.Width, (0.6f * (1000 - value_1P1[1]) * this.Height) / 1000.0f + Y_axis_Zero,
                        0.08f * this.Width, 0.6f * value_1P1[1] * this.Height / 1000.0f);
            graph.FillRectangle(new SolidBrush(curC_Color), 0.41f * this.Width, (0.6f * (1000 - value_1P1[2]) * this.Height) / 1000.0f + Y_axis_Zero,
                        0.08f * this.Width, 0.6f * value_1P1[2] * this.Height / 1000.0f);

            for (int cou_1P1 = 0; cou_1P1 < 3; cou_1P1++)
            {
                graph.DrawString(value_1P1[cou_1P1].ToString("F2"), font, textBrush,
                    0.35f * this.Width, (0.79f + 0.06f * cou_1P1) * this.Height);
            }

            e.Dispose();
        }

        public void Drawing1P11Value(object sender, PaintEventArgs e)
        {
            Graphics graph = e.Graphics;
            ///
            /// 1P11的柱状图
            ///
            graph.FillRectangle(new SolidBrush(curA_Color), 0.5f * this.Width, (0.6f * (1000.0f - value_1P11[0]) * this.Height) / 1000.0f + Y_axis_Zero,
                        0.08f * this.Width, (0.6f * value_1P11[0] * this.Height) / 1000.0f);
            graph.FillRectangle(new SolidBrush(curB_Color), 0.58f * this.Width, (0.6f * (1000.0f - value_1P11[1]) * this.Height) / 1000.0f + Y_axis_Zero,
                        0.08f * this.Width, (0.6f * value_1P11[1] * this.Height) / 1000.0f);
            graph.FillRectangle(new SolidBrush(curC_Color), 0.66f * this.Width, (0.6f * (1000.0f - value_1P11[2]) * this.Height) / 1000.0f + Y_axis_Zero,
                        0.08f * this.Width, (0.6f * value_1P11[2] * this.Height) / 1000.0f);

            for (int cou_1P11 = 0; cou_1P11 < 3; cou_1P11++)
            {
                graph.DrawString(value_1P11[cou_1P11].ToString("F2"), font, textBrush,
                    0.6f * this.Width, (0.79f + 0.06f * cou_1P11) * this.Height);
            }
            e.Dispose();
        }

        public void Drawing2P1Value(object sender, PaintEventArgs e)
        {
            Graphics graph = e.Graphics;
            ///
            /// 2P1的柱状图
            ///
            graph.FillRectangle(new SolidBrush(curA_Color), 0.75f * this.Width, (0.6f * (1000 - value_2P1[0]) * this.Height) / 1000.0f + Y_axis_Zero,
                        0.08f * this.Width, 0.6f * value_2P1[0] * this.Height / 1000.0f);
            graph.FillRectangle(new SolidBrush(curB_Color), 0.83f * this.Width, (0.6f * (1000 - value_2P1[1]) * this.Height) / 1000.0f + Y_axis_Zero,
                        0.08f * this.Width, 0.6f * value_2P1[1] * this.Height / 1000.0f);
            graph.FillRectangle(new SolidBrush(curC_Color), 0.91f * this.Width, (0.6f * (1000 - value_2P1[2]) * this.Height) / 1000.0f + Y_axis_Zero,
                        0.08f * this.Width, 0.6f * value_2P1[2] * this.Height / 1000.0f);

            for (int cou_2P1 = 0; cou_2P1 < 3; cou_2P1++)
            {
                graph.DrawString(value_2P1[cou_2P1].ToString("F2"), font, textBrush,
                    0.85f * this.Width, (0.79f + 0.06f * cou_2P1) * this.Height);
            }
            e.Dispose();
        }
    }
}
